//
// Copyright 2022 Google Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

// stylelint-disable selector-class-pattern --
// Selector '.mdc-*' should only be used in this project.

@use '@material/button/button-text-theme';
@use '@material/elevation/elevation-theme';
@use '@material/elevation/mixins' as elevation-mixins;
@use '@material/feature-targeting/feature-targeting';
@use '@material/icon-button/icon-button-theme';
@use '@material/rtl/rtl';
@use '@material/shape/mixins' as shape-mixins;
@use '@material/theme/keys';
@use '@material/theme/theme';
@use '@material/theme/theme-color';
@use '@material/tokens/resolvers';
@use '@material/typography/typography';
@use 'sass:color';
@use 'sass:map';
@use 'sass:meta';

$fill-color: color.mix(
  theme-color.prop-value(on-surface),
  theme-color.prop-value(surface),
  80%
) !default;
$label-ink-color: rgba(
  theme-color.prop-value(surface),
  theme-color.text-emphasis(high)
) !default;
$action-ink-color: #bb86fc !default;
$dismiss-ink-color: rgba(
  theme-color.prop-value(surface),
  theme-color.text-emphasis(high)
) !default;

$label-type-scale: body2 !default;
$dismiss-icon-size: 18px !default;
$dismiss-button-size: 36px !default;
$min-width: 344px !default;
$max-width: 672px !default;
$mobile-breakpoint: 480px !default;
$viewport-margin-narrow: 8px !default;
$viewport-margin-wide: 24px !default;
$padding: 8px !default;

$elevation: 6 !default;
$shape-radius: small !default; // Key from shape.$category-keywords or CSS length value (e.g., 4px)
$z-index: 8 !default; // One above mdc-dialog

// These variables need to be kept in sync with the values in constants.js.
$enter-duration: 150ms !default;
$exit-duration: 75ms !default;

$custom-property-prefix: 'snackbar';

$light-theme: (
  action-focus-label-text-color: null,
  action-focus-state-layer-color: null,
  action-focus-state-layer-opacity: 0.12,
  action-hover-label-text-color: null,
  action-hover-state-layer-color: null,
  action-hover-state-layer-opacity: 0.04,
  action-label-text-color: null,
  action-label-text-font: null,
  action-label-text-size: null,
  action-label-text-tracking: null,
  action-label-text-weight: null,
  action-pressed-label-text-color: null,
  action-pressed-state-layer-color: null,
  action-pressed-state-layer-opacity: 0.1,
  container-color: null,
  container-elevation: null,
  container-shadow-color: null,
  container-shape: null,
  icon-color: null,
  icon-focus-icon-color: null,
  icon-focus-state-layer-color: null,
  icon-focus-state-layer-opacity: 0.12,
  icon-hover-icon-color: null,
  icon-hover-state-layer-color: null,
  icon-hover-state-layer-opacity: 0.04,
  icon-pressed-icon-color: null,
  icon-pressed-state-layer-color: null,
  icon-pressed-state-layer-opacity: 0.1,
  icon-size: 24px,
  supporting-text-color: theme-color.$on-surface,
  supporting-text-font: typography.get-font(body2),
  supporting-text-line-height: typography.get-line-height(body2),
  supporting-text-size: typography.get-size(body2),
  supporting-text-tracking: typography.get-tracking(body2),
  supporting-text-weight: typography.get-weight(body2),
);

@mixin theme($theme, $resolvers: resolvers.$material) {
  @include theme.validate-theme($light-theme, $theme);
  $theme: _resolve-theme($theme, $resolvers);
  @include keys.declare-custom-properties(
    $theme,
    $prefix: $custom-property-prefix
  );
}

@mixin theme-styles($theme, $resolver: resolvers.$material) {
  @include theme.validate-theme-styles($light-theme, $theme);

  $theme: keys.create-theme-properties(
    $theme,
    $prefix: $custom-property-prefix
  );

  .mdc-snackbar__action {
    @include button-text-theme.theme-styles(
      (
        focus-label-text-color: map.get($theme, action-focus-label-text-color),
        focus-state-layer-color: map.get($theme, action-focus-state-layer-color),
        focus-state-layer-opacity:
          map.get($theme, action-focus-state-layer-opacity),
        hover-label-text-color: map.get($theme, action-hover-label-text-color),
        hover-state-layer-color: map.get($theme, action-hover-state-layer-color),
        hover-state-layer-opacity:
          map.get($theme, action-hover-state-layer-opacity),
        label-text-color: map.get($theme, action-label-text-color),
        label-text-font: map.get($theme, action-label-text-font),
        label-text-size: map.get($theme, action-label-text-size),
        label-text-tracking: map.get($theme, action-label-text-tracking),
        label-text-weight: map.get($theme, action-label-text-weight),
        pressed-label-text-color:
          map.get($theme, action-pressed-label-text-color),
        pressed-state-layer-color:
          map.get($theme, action-pressed-state-layer-color),
        pressed-state-layer-opacity:
          map.get($theme, action-pressed-state-layer-opacity),
      )
    );
  }

  .mdc-snackbar__dismiss {
    @include icon-button-theme.theme-styles(
      (
        icon-size: map.get($theme, icon-size),
        focus-icon-color: map.get($theme, icon-focus-icon-color),
        focus-state-layer-color: map.get($theme, icon-focus-state-layer-color),
        focus-state-layer-opacity:
          map.get($theme, icon-focus-state-layer-opacity),
        hover-icon-color: map.get($theme, icon-hover-icon-color),
        hover-state-layer-color: map.get($theme, icon-hover-state-layer-color),
        hover-state-layer-opacity:
          map.get($theme, icon-hover-state-layer-opacity),
        icon-color: map.get($theme, icon-color),
        pressed-icon-color: map.get($theme, icon-pressed-icon-color),
        pressed-state-layer-color:
          map.get($theme, icon-pressed-state-layer-color),
        pressed-state-layer-opacity:
          map.get($theme, icon-pressed-state-layer-opacity),
      )
    );
  }

  @include fill-color(map.get($theme, container-color));
  @include shape-radius(map.get($theme, container-shape));
  @include _container-elevation(
    $resolver,
    map.get($theme, container-elevation),
    map.get($theme, container-shadow-color)
  );
  @include label-ink-color(map.get($theme, supporting-text-color));
  @include _supporting-text-typography(
    (
      font: map.get($theme, supporting-text-font),
      size: map.get($theme, supporting-text-size),
      tracking: map.get($theme, supporting-text-tracking),
      weight: map.get($theme, supporting-text-weight),
      line-height: map.get($theme, supporting-text-line-height),
    )
  );
}

@function _resolve-theme($theme, $resolvers) {
  @return map.merge(
    $theme,
    _resolve-theme-elevation(
      $theme,
      map.get($resolvers, elevation),
      container-elevation
    )
  );
}

@function _resolve-theme-elevation($theme, $resolver, $keys...) {
  @if $resolver == null {
    @return $theme;
  }

  @each $key in $keys {
    // Resolve the value for each state key.
    $resolved-value: meta.call(
      $resolver,
      $elevation: map.get($theme, $key),
      $shadow-color: map.get($theme, container-shadow-color)
    );

    // Update the theme with the resolved value.
    $theme: map.set($theme, $key, $resolved-value);
  }

  @return $theme;
}

@mixin fill-color($color, $query: feature-targeting.all()) {
  $feat-color: feature-targeting.create-target($query, color);

  .mdc-snackbar__surface {
    @include feature-targeting.targets($feat-color) {
      @include theme.property(background-color, $color);
    }
  }
}

@mixin label-ink-color($color, $query: feature-targeting.all()) {
  $feat-color: feature-targeting.create-target($query, color);

  .mdc-snackbar__label {
    @include feature-targeting.targets($feat-color) {
      @include theme.property(color, $color);
    }
  }
}

@mixin shape-radius(
  $radius,
  $rtl-reflexive: false,
  $query: feature-targeting.all()
) {
  .mdc-snackbar__surface {
    @include shape-mixins.radius($radius, $rtl-reflexive, $query: $query);
  }
}

@mixin min-width(
  $min-width,
  $mobile-breakpoint: $mobile-breakpoint,
  $query: feature-targeting.all()
) {
  $feat-structure: feature-targeting.create-target($query, structure);

  .mdc-snackbar__surface {
    @include feature-targeting.targets($feat-structure) {
      min-width: $min-width;

      // The first media query ensures that snackbars are always 100% width on mobile devices, as required by the spec.
      // The second media query prevents snackbars from being wider than the viewport for large min-width values.
      @media (max-width: $mobile-breakpoint), (max-width: $min-width) {
        min-width: 100%;
      }
    }
  }
}

@mixin max-width($max-width, $query: feature-targeting.all()) {
  $feat-structure: feature-targeting.create-target($query, structure);

  .mdc-snackbar__surface {
    @include feature-targeting.targets($feat-structure) {
      max-width: $max-width;
    }
  }
}

@mixin elevation($z-index, $query: feature-targeting.all()) {
  .mdc-snackbar__surface {
    @include elevation-mixins.elevation($z-index, $query: $query);
  }
}

@mixin viewport-margin($margin, $query: feature-targeting.all()) {
  $feat-structure: feature-targeting.create-target($query, structure);

  @include feature-targeting.targets($feat-structure) {
    margin: $margin;
  }
}

@mixin z-index($z-index, $query: feature-targeting.all()) {
  $feat-structure: feature-targeting.create-target($query, structure);

  @include feature-targeting.targets($feat-structure) {
    z-index: $z-index;
  }
}

@mixin position-leading($query: feature-targeting.all()) {
  $feat-structure: feature-targeting.create-target($query, structure);

  @include feature-targeting.targets($feat-structure) {
    justify-content: flex-start;
  }
}

@mixin layout-stacked($query: feature-targeting.all()) {
  $feat-structure: feature-targeting.create-target($query, structure);

  .mdc-snackbar__label {
    @include feature-targeting.targets($feat-structure) {
      @include rtl.reflexive-property(padding, 16px, 8px);
      padding-bottom: 12px;
    }
  }

  .mdc-snackbar__surface {
    @include feature-targeting.targets($feat-structure) {
      flex-direction: column;
      align-items: flex-start;
    }
  }

  .mdc-snackbar__actions {
    @include feature-targeting.targets($feat-structure) {
      align-self: flex-end;
      margin-bottom: $padding;
    }
  }
}

@mixin _container-elevation($resolver, $elevation, $shadow-color) {
  $elevation-resolver: map.get($resolver, elevation);

  @include elevation-theme.with-resolver(
    $elevation-resolver,
    $elevation: $elevation,
    $shadow-color: $shadow-color
  );
}

@mixin _supporting-text-typography($typography-map) {
  $font: map.get($typography-map, font);
  $size: map.get($typography-map, size);
  $tracking: map.get($typography-map, tracking);
  $weight: map.get($typography-map, weight);
  $line-height: map.get($typography-map, line-height);

  .mdc-snackbar__label {
    @include theme.property(letter-spacing, $tracking);
    @include theme.property(font-size, $size);
    @include theme.property(font-family, $font);
    @include theme.property(font-weight, $weight);
    @include theme.property(line-height, $line-height);
  }
}
